package create.he.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

import create.he.builder.GeneratorBuilder;
import create.he.common.Constant;
import create.he.entity.GeneratorContent;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
/**
 * 创建文件的 的工具
 * 
 * @author <a href="http://szmirren.com">Mirren</a>
 *
 */
public class CreateFileUtil {
 	/**
	 * 执行创建文件
	 * 
	 * @param content
	 *          模板所需要的上下文
	 * @param templeteName
	 *          模板的名字 示例:Entity.ftl
	 * @param packageName
	 *          包名 示例:com.szmirren
	 *   @param        outFilePath
	 *   	文件输出路径
	 * @param fileName
	 *          文件名 示例:Entity.java
	 * @param	version
	 * 		模板路径
	 * @throws Exception
	 */
	public static void createFile(GeneratorContent content, String templeteName,  String packageName, String outFilePath,String fileName ,String version) throws Exception {
		String outputPath =outFilePath+ "/"  + packageName.replace(".", "/") + "/";
		Configuration config = new Configuration(Configuration.VERSION_2_3_23);
		 
		String tempPath =System.getProperty("user.dir")+"/src/main/resources/"+Constant.TEMPLATE_DIR_NAME+"/";
		config.setDirectoryForTemplateLoading(new File(tempPath));
		config.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
		config.setDefaultEncoding("utf-8");
		Template template = config.getTemplate(templeteName);
		Map<String, Object> item = new HashMap<>();
		item.put("content", content);
		item.put("author", System.getProperty("user.name"));
		Files.createDirectories(Paths.get(outputPath));
		try (Writer writer = new OutputStreamWriter(new FileOutputStream(outputPath + fileName+Constant.JAVA_SUFFIX))) {
			template.process(item, writer);
		}
		System.out.println(packageName+"\t"+fileName);
	}
	/***
	 *  执行创建文件（默认类名，包路径）
	 * @param builder 构建类
	 * @param templeteName  模板的名字 示例:Entity.ftl
	 * @param cla 模板对应的类
	 * @throws Exception
	 */
	
	public static void createFile(GeneratorBuilder builder, String templeteName, Class<?> cla) throws Exception {
		createFile(builder, templeteName, cla, "");
	}
	/**
	 * 执行创建文件（默认类名，包路径）
	 * @param builder 构建类
	 * @param templeteName 模板的名字 示例:Entity.ftl
	 * @param cla 模板对应的类
	 * @param version 模板所用的版本
	 * @throws Exception
	 */
	public static void createFile(GeneratorBuilder builder, String templeteName, Class<?> cla,String version) throws Exception {
		GeneratorContent generatorContent = builder.getGeneratorContent();
		Class generator = generatorContent.getClass();
		Field field = generator.getDeclaredField(StrUtil.unlineToCamel(cla.getSimpleName()));
		field.setAccessible(true);
		Object object =  field.get(generatorContent);
		cla.newInstance();
		
		String packageName = (String) cla.getMethod("getClassPackage").invoke(object);
		String fileName =	(String) cla.getMethod("getClassName").invoke(object);
		
		createFile(generatorContent, templeteName,packageName,  builder.getOutPath(), fileName,version);
	}
}
